<?php

class Application_Model_WxclientMapper
{
	protected $_dbTable;

	public function setDbTable ($dbTable) {
		if (is_string($dbTable))
		$dbTable = new $dbTable ();
		if (!$dbTable instanceof Zend_Db_Table_Abstract)
		throw new Exception ('Invalid table data gateway provided');
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable () {
		if (null === $this->_dbTable) {
			$this->setDbTable ('Application_Model_DbTable_WXclient');
		}
		return $this->_dbTable;
	}

	public function addRef($openId, $siteId) {
		if(empty($openId) || empty($siteId))
		  return false;
		$userinfo = array (
			'openid' => $openId,
			'clientid' => $siteId
		);
		return $this->getDbTable()->insert($userinfo);
	}
	
	public function checkRepeat($openId,$shopId){
		$adapter = $this->getDbTable()->getAdapter();
		$where = $adapter->quoteInto('openid = ?', $openId)	
				. $adapter->quoteInto('AND clientid = ?', $shopId);	
		$result = $this->getDbTable()->fetchAll($where);
		$res =$result->toArray();
		if(!empty($res)){
			return true;
		}else{
			return false;
		}
	}
	public function getShopIdList($openId){//从数据库查询数据
		$where = $this->getDbTable()->getAdapter()->quoteInto('openid = ?', $openId);
		$result = $this->getDbTable()->fetchAll($where);
		return $result->toArray();
	}
	public function find_openid($openid){//从数据库查询clientid
		$select = $this->getDbTable()->select();
		$select->from($this->getDbTable(),'count(openid)');
		$select->where('openid = ?',$openid); 
		$sql = $select->__toString();
		$result = $this->getDbTable()->getAdapter()->fetchAll($sql);
		return $result;
	}
	
}
?>